/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.framework.workflow.controller;

import com.framework.common.domain.ResultData;
import com.framework.workflow.params.XmlModelParam;
import com.framework.workflow.service.IModuleService;
import com.framework.workflow.service.IResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/modeler")
@Api(tags = "模型保存")
public class ResourceController {
  @Autowired
  IResourceService resourceService;
  @Autowired
  private IModuleService modulesService;

  @RequestMapping(value="/{modelId}/save", method = RequestMethod.POST)
  @ApiOperation(value = "保存模型")
  public ResultData<String> saveXml(@PathVariable String modelId, @RequestBody XmlModelParam model) {
    resourceService.save(modelId, model.getName(), model.getXml());
    modulesService.updateProcDefKey(modelId, model.getName());
    return ResultData.success();
  }

  @GetMapping(value="/{modelId}")
  @ApiOperation(value = "获取模型")
  public ResultData<String> getXml(@PathVariable String modelId) {
    String xml = resourceService.getXml(modelId);
    return ResultData.data(xml);
  }
}
